In [5]:
x = 'foo'
print(id(x))
print(id(x.upper()))
print(id(x + 'bar'))
In [6]:
x = 'foo'
y = x
print(x, id(x))
x = 'bar'
print(x, id(x))# objekt foo sa nezmenil, to len x uz smeruje na iny objekt
print(y, id(y))
In [ ]:
# -- JAVA --
final List<Integer> list = new ArrayList<Integer>();
list = new ArrayList<Integer>(); // toto sa neskompiluje
In [ ]:
# -- JAVA --
final List<Integer> list = new ArrayList<Integer>();
list.add(1); //toto prejde bez problemov
In [ ]:
# -- JAVA --
final List<Integer> list = Collections.unmodifiableList(new ArrayList<Integer>(...)); //toto je immutable list
In [14]:
x = 'foo'
y = x
print(x) # foo
y += 'bar'
print(x) # foo
print(y)
In [15]:
x = [1, 2, 3]
y = x
print(x)
y += [3, 2, 1]
print(x)
In [16]:
def func(val):
val += 'bar'
x = 'foo'
print(x)
func(x)
print(x)
In [17]:
def func(val):
val += [3, 2, 1]
x = [1, 2, 3]
print(x)
func(x)
print(x)
In [18]:
a = 'text'
print(a)
print('Adresa je: {}'.format(id(a)))
In [19]:
# Znamena to, ze neviem menit hodnotu
a[0] = 'T'
print(a)
print('Adresa je: {}'.format(id(a)))
In [20]:
a = [1,2,3,4,5]
print(a)
print('Adresa je: {}'.format(id(a)))
In [21]:
# Znamena to, ze neviem menit hodnotu
a[0] = 'T'
print(a)
print('Adresa je: {}'.format(id(a)))
In [22]:
t1 = (1, 2, 3, 4, 5)
t1
Out[22]:
In [23]:
t1[1]
Out[23]:
In [24]:
t1[1]=3
In [25]:
t1 = (1, 2, 3, 4, 5)
# Ked chceme update, treba vyrobit novy objekt
t2 = t1[:2] + (17, ) + t1[3:]
t2
Out[25]:
In [26]:
# alebo
l1 = list(t1)
l1[2] = 17
t2 = tuple(l1)
t2
Out[26]:
In [27]:
# vs.
a = [1,2,3,4,5]
a[2] = 17
a
Out[27]:
In [28]:
# inspirovane https://www.youtube.com/watch?v=5qQQ3yzbKp8
employees = ['Jozo', 'Eva', 'Fero', 'Miro', 'Anna', 'Kristina']
output = '<ul>\n'
for employee in employees:
output += '\t<li>{}</li>\n'.format(employee)
# print('Adresa outputu je: {}'.format(id(output)))
output += '</ul>'
print(output)
In [30]:
import pyrsistent as ps
In [31]:
v1 = ps.pvector([1, 2, 3, 4])
v1 == ps.v(1, 2, 3, 4)
Out[31]:
In [32]:
v1[1]
Out[32]:
In [33]:
v1[1:3]
Out[33]:
In [34]:
v1[1] = 3
In [35]:
v3 = v1.set(1, 5)
print(v3)
print(v1)
In [36]:
m1 = ps.pmap({'a':1, 'b':2})
m1 == ps.m(a=1, b=2)
Out[36]:
In [37]:
m1['a']
Out[37]:
In [38]:
m1.b # toto s dict nejde
Out[38]:
In [39]:
print(m1.set('a', 3))
print(m1)
In [40]:
print(id(m1), id(m1.set('a', 3)))
In [41]:
ps.freeze([1, {'a': 3}])
Out[41]:
In [42]:
ps.thaw(ps.v(1, ps.m(a=3)))
Out[42]:
https://github.com/tobgu/pyrsistent
In [43]:
v1 = ps.v(0, 1, 2, 3, 4, 5, 6, 7, 8)
print(v1)
v2 = v1.set(5, 'beef')
print(v2)
pvector([0, 1, 2, 3, 4, 5, 6, 7, 8])
pvector([0, 1, 2, 3, 4, 'beef', 6, 7, 8])
In [44]:
def process_item(x):
return x*x
item_list = [1,2,3,4,5,6]
In [45]:
# impertivny zapis
collection = []
for item in item_list:
partial_result = process_item(item)
collection.append(partial_result)
collection
Out[45]:
In [46]:
# C-like zapis
collection = []
index = 0
while index < len(item_list):
partial_result = process_item(item_list[index])
collection.append(partial_result)
index += 1
collection
Out[46]:
In [47]:
def process_item(x):
return x*x
item_list = [1,2,3,4,5,6]
In [48]:
# funkcionalny zapis
collection = map(process_item, item_list)
collection
Out[48]:
In [49]:
def fahrenheit(T):
return ((float(9)/5)*T + 32)
def celsius(T):
return (float(5)/9)*(T-32)
temperatures = (36.5, 37, 37.5, 38, 39)
F = list(map(fahrenheit, temperatures))
C = list(map(celsius, F))
print(F)
print(C)
In [50]:
list(map(len, open('data/morho.txt')))
Out[50]:
In [51]:
list(map(print, open('data/morho.txt')))
Out[51]:
In [52]:
def my_map(f, seq): # Takto by to mohlo byt v pythone 2 a nie 3. Tam map vracia iterator.
result = []
for x in seq:
result.append(f(x))
return result
In [53]:
item_list = [1,2,3,4,5,6]
def condition(x):
return(x % 2 == 0)
In [54]:
collection = []
for item in item_list:
if condition(item):
collection.append(item)
collection
Out[54]:
In [55]:
item_list = [1,2,3,4,5,6]
def condition(x):
return(x % 2 == 0)
In [56]:
collection = filter(condition, item_list)
list(collection)
Out[56]:
In [57]:
fibonacci = [0,1,1,2,3,5,8,13,21,34,55]
def is_even(x):
return x % 2 == 0
list(filter(is_even, fibonacci))
Out[57]:
In [58]:
item_list = [47,11,42,13]
def add(a,b):
return(a+b)
In [59]:
from functools import reduce
reduce(add, item_list)
Out[59]:
In [60]:
total = 0 # Takto by to bolo imperativne
for item in item_list:
total = add(total, item)
total
Out[60]:
In [61]:
from functools import reduce
def mul(a,b):
return a * b
reduce(mul, [1,2,3,4,5])
Out[61]:
In [62]:
from operator import add
In [63]:
from operator import mul
In [64]:
from functools import reduce
from operator import add
print(reduce(add, open('data/morho.txt')))
In [65]:
from operator import or_
reduce(or_, ({1}, {1, 2}, {1, 3})) # union
Out[65]:
In [66]:
from operator import and_
reduce(and_, ({1}, {1, 2}, {1, 3}))
Out[66]:
In [67]:
my_sum = lambda x, y: x + y
my_sum(1,2)
Out[67]:
In [68]:
item_list = [1,2,3,4,5]
print(list(map(lambda x: x**2, item_list)))
In [69]:
item_list = ["auto", "macka", "traktor"]
list(map(lambda x: x.upper(), item_list))
Out[69]:
In [70]:
print(list(map(lambda x: x**2, [1,2,3,4,5])))
print([x**2 for x in [1,2,3,4,5]])
In [71]:
print(list(filter(lambda x: x % 2 == 0, [1,2,3,4,5])))
print([x for x in [1,2,3,4,5] if x % 2 == 0])
Celkom pekny priklad na jednoduchu MapReduce ulohu v Pythone.
Klasicky Word count priklad
http://www.michael-noll.com/tutorials/writing-an-hadoop-mapreduce-program-in-python/
--- pseudokod --- function map(String name, String document):
// name: document name
// document: document contents
for each word w in document:
emit (w, 1)
function reduce(String word, Iterator partialCounts):
// word: a word
// partialCounts: a list of aggregated partial counts
sum = 0
for each pc in partialCounts:
sum += pc
emit (word, sum)